package controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import model.Blog;
import model.BlogDao;
import model.User;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.List;

/*
处理博客的相关操作
 */
@WebServlet("/list")
public class BlogList extends HttpServlet {
    private ObjectMapper objectMapper = new ObjectMapper();
    // 获取到数据库中的博客列表
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("utf8");
        BlogDao blogDao = new BlogDao();
        // 先获取请求的参数,如果存在,说明是要详情页,否则就是列表
        String id = req.getParameter("blogId");
        String title = req.getParameter("searchTitle");
        if (id != null){
            // 存在,则获取详情页
            int blogId = Integer.parseInt(id);
            Blog blog = blogDao.selectOneBlog(blogId);
            // 转为JSON格式
            String blogString = objectMapper.writeValueAsString(blog);
            resp.setContentType("application/json; charset=utf-8");
            resp.getWriter().write(blogString);
        }else if (title != null){
            if (title.equals("%")){
                title = "\\%";
            }
            List<Blog> blogs = blogDao.selectBlogsByTitle(title);
            // 转为JSON格式
            String blogsString = objectMapper.writeValueAsString(blogs);
            resp.setContentType("application/json; charset=utf-8");
            resp.getWriter().write(blogsString);
        }else {
            // 不存在,则获取列表
            List<Blog> blogs = blogDao.selectAllBlog();// 得到所有的博客信息
            // 转为JSON格式
            String blogsString = objectMapper.writeValueAsString(blogs);
            resp.setContentType("application/json; charset=utf-8");
            resp.getWriter().write(blogsString);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf8");
        String blogId = req.getParameter("blogId");
        BlogDao blogDao = new BlogDao();

        // 提交博客
        Blog blogReq = objectMapper.readValue(req.getInputStream(), Blog.class);
        String title = blogReq.getTitle();
        String content = blogReq.getContent();
        if (title == null || "".equals(title)){
            resp.getWriter().write("titleNull");
            return;
        }
        if (content == null || "".equals(content)){
            resp.getWriter().write("contentNull");
            return;
        }
        // 得到 user对象
        HttpSession session = req.getSession(false);
        User user = (User)session.getAttribute("user");
        // 构造 blog对象
        Blog blog = new Blog();
        blog.setTitle(title);
        blog.setContent(content);
        blog.setUserId(user.getUserId());

        // 更新博客
        if (blogId != null){
            blog.setBlogId(Integer.parseInt(blogId));
            blogDao.updateBlog(blog);
        }else {
            blogDao.insertBlog(blog);
        }
        resp.getWriter().write("success");
    }
}
